import axios, { type AxiosRequestConfig, type Method } from 'axios'

export type RequestConfig = AxiosRequestConfig
export type TMethod = Method

export const TIMEOUT = 5 * 1000

// 超时时间
axios.defaults.timeout = TIMEOUT
// 请求携带cookie
axios.defaults.withCredentials = true

let isLockdown = false

function setLockdown(response: any) {
  if (response?.status === 403) {
    isLockdown = true
    return
  }
  if (isLockdown) {
    isLockdown = false
  }
}
export function getLockDown() {
  return isLockdown
}

axios.interceptors.request.use(
  (config) => {
    return config
  },
  (error) => {
    return Promise.reject(error)
  },
)

// 统一处理结果
axios.interceptors.response.use(
  (response) => {
    if (response?.status == 200) {
      return response.data
    } else {
      // ...
    }
  },
  (error) => {
    return Promise.reject(error)
  },
)

export { axios as http }
export default axios
